<template>
  <div class="app-container">
    <!-- 记录类型选择 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px" class="mb4">
      <el-form-item label="记录类型" prop="recordType">
        <el-select v-model="recordType" placeholder="请选择记录类型" @change="handleRecordTypeChange">
          <el-option label="收藏记录" value="collect" />
          <el-option label="点赞记录" value="like" />
          <el-option label="浏览记录" value="look" />
          <el-option label="厌弃记录" value="nasty" />
          <el-option label="评分记录" value="score" />
          <el-option label="分享记录" value="share" />
        </el-select>
      </el-form-item>

      <!-- 公共查询条件 -->
      <el-form-item label="记录ID" prop="recordId">
        <el-input v-model="queryParams.recordId" placeholder="请输入记录ID" />
      </el-form-item>


      <el-form-item label="发布用户" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入用户名" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 记录表格 -->
    <el-table :select-on-indeterminate="false" v-loading="loading" :data="tableList">
      <el-table-column label="记录ID" align="center" prop="recordId" />
      <el-table-column label="发布用户" align="center" prop="createBy" />
      <el-table-column label="记录IP" align="center" prop="ip" />
      <!-- 评分记录特有列 -->
      <el-table-column v-if="recordType === 'score'" label="评分" align="center" prop="score" />
      <!-- 分享记录特有列 -->
      <el-table-column v-if="recordType === 'share'" label="分享地址" align="center" prop="shareUrl" />
      <el-table-column label="状态" align="center" prop="visible" />
      <el-table-column label="系统" align="center" prop="userSystem" />
      <el-table-column label="浏览器" align="center" prop="userBower" />
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="RecordManagement">
import { ref, reactive, toRefs, getCurrentInstance } from 'vue';
import RightToolbar from '@/components/RightToolbar/index.vue';
import Pagination from '@/components/Pagination/index.vue';
import {
  listRecordCount,
  listCollectRecord,
  listLikeRecord,
  listLookRecord,
  listNastyRecord,
  listScoreRecord,
  listShareRecord
} from "@/api/cms/record";

const { proxy } = getCurrentInstance();

// 记录类型和ID
const recordType = ref('collect');
const showSearch = ref(true);

// 统一的表格数据
const tableList = ref([]);
const loading = ref(true);
const total = ref(0);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    createBy: null,
    recordId: null // 对于非文章统计类型使用
  }, rules: {}
});
const { queryParams } = toRefs(data);

// 记录类型对应的API映射
const recordTypeApiMap = {
  'collect': listCollectRecord,
  'like': listLikeRecord,
  'look': listLookRecord,
  'nasty': listNastyRecord,
  'score': listScoreRecord,
  'share': listShareRecord
};

// 处理记录类型变更
function handleRecordTypeChange() {
  queryParams.value.pageNum = 1;
  getList();
}

// 获取数据列表
function getList() {
  loading.value = true;
  const api = recordTypeApiMap[recordType.value];

  // 根据记录类型构建查询参数
  const params = { ...queryParams.value };


  api(params).then((response) => {
    tableList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  });
}

// 处理搜索
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 重置搜索
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize = 10;
  getList();
}

// 初始加载数据
getList();
</script>

<style scoped>
.content-container {
  padding: 10px 0;
}
</style>